<?php
	class userpages extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->model('pagesModel');
			
			$this->pages = array(
				'about'			=>	'about us',
				'vendor'		=>	'vendor programme',
				'career'		=>	'career',
				'advertising'	=>	'advertising',
			);
			
			$user =& Users::getInstance();
			
			if ($user->level < 1) {
				recirect('/');
			}
			
			$this->load->helper('url');
			$this->load->helper('form');
			$this->load->model('Jobs');
		}
		
		public function index()
		{
			$this->load->view('admin/userpages.php');
		}
		
		public function edit($page)
		{
			$this->load->library('Menu');
			$menu = new Menu;
			$user =& Users::getInstance();
			$data = $menu->gen();
			$data['user'] = $user;
			$data['pageEditing'] = str_replace(' ', '-', $this->pages[$page]);
			$data['pageEditingName'] = ucwords($this->pages[$page]);
			
			$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
			$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
			
			if(isset($page) && (strlen($page) > 0) && array_key_exists($page, $this->pages))
			{
				$query = $this->pagesModel->find($page);
				$data['page'] = $query;
				
				if(isset($_POST['submit']))
				{
					if($this->pagesModel->save($page))
					{
						redirect('edit/'.strtolower(str_replace(' ', '-', $data['pageEditingName'])));
					}
				}
				
				$this->load->view('edit-page.php', $data);
			}
		}
	}
?>